package com.monster.completable.future;

import com.monster.threadpool.MyThreadPool;

import java.util.concurrent.*;

/**
 * description:
 * runAsync系列方法实现了无返回值的异步计算，当想异步执行一个任务，并且不需要任务的执行结果时可以使用该方法
 * @author Yuanml on 2021/9/19 14:09
 */
public class RunAsync {
    public void studyRunAsync() throws ExecutionException, InterruptedException {
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("over");
        });

        System.out.println(future.get());
    }

    public void runAsyncWithCustomThreadPool() throws ExecutionException, InterruptedException {
        CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("over");
        }, MyThreadPool.getThreadPool());
        System.out.println(future.get());
    }
}
